package com.maoyu.cloud.role.service.impl;

import com.maoyu.cloud.mybatis.entity.PtRoleEntity;
import com.maoyu.cloud.mybatis.entity.ZtreeEntity;
import com.maoyu.cloud.mybatis.flexigrid.PadingRstType;
import com.maoyu.cloud.mybatis.flexigrid.PagingBean;
import com.maoyu.cloud.role.entity.SearchRoleEntity;
import com.maoyu.cloud.role.respository.RoleDao;
import com.maoyu.cloud.role.service.RoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleDao roleDao;

    @Override
    public PadingRstType<PtRoleEntity> queryRoleListByPage(SearchRoleEntity entity, PagingBean paging) {
        paging.deal(PtRoleEntity.class);
        PadingRstType<PtRoleEntity> padingRstType = new PadingRstType<>();
        padingRstType.setPage(paging.getPage());
        Integer total = roleDao.queryRoleListTotal(entity);
        padingRstType.setTotal(total);
        List<PtRoleEntity> list = roleDao.queryRoleListByPage(entity,paging);
        padingRstType.setRawRecords(list);
        padingRstType.putItems();
        return padingRstType;
    }

    @Override
    public List<ZtreeEntity> queryZtreeByRoleId(String roleUuid) {
        List<ZtreeEntity> list = roleDao.queryMenuZtreeList();
        List<String> menuIds = roleDao.queryMenuByRole(roleUuid);
        for(ZtreeEntity item:list){
            for(String menuId:menuIds){
                if(menuId.equals(item.getId())){
                    item.setChecked(true);
                    break;
                }
            }
        }
        return list;
    }


    @Override
    public void saveMenuRefRole(String menuIdArray, String roleUuidArray) {
        String[] roleUuids = roleUuidArray.split(",");
        String[] menuIds = menuIdArray.split(",");
        for(String roleUuid:roleUuids){
            roleDao.deleteMenuByRole(roleUuid);
            for(String menuId:menuIds){
                roleDao.insertRoleRefMenu(menuId,roleUuid);

            }
        }
    }

    @Override
    public List<ZtreeEntity> queryMenuZtreeAll() {
        List<ZtreeEntity> list = roleDao.queryMenuZtreeList();
        return list;
    }

    @Override
    public void addroleInfoById(PtRoleEntity entity) {
        roleDao.addroleInfoById(entity);
    }

    @Override
    public void deleteRoleById(String roleUuid) {
        roleDao.deleteRoleInfo(roleUuid);
    }

    /**
     * 修改角色信息
     * @param entity 角色信息
     */
    @Override
    public void updateRoleById(PtRoleEntity entity) {
        roleDao.updateRoleInfo(entity);
    }

    /**
     * 根据角色id查询角色信息
     * @param roleUuid 角色id
     * @return 角色信息
     */
    @Override
    public PtRoleEntity queryRoleById(String roleUuid) {
        return roleDao.queryRoleById(roleUuid);
    }

}
